package com.woniu.service.impl;

import com.woniu.entity.User;
import com.woniu.mapper.UserMapper;
import com.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//@EnableCaching
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    @Cacheable(value = "users",key = "#root.methodName")
    public List<User> findAll() {
        return userMapper.findAll();
    }
    @Cacheable(value = "userSearch", key = "#username")
    @Override
    public User findUserByUsername(String username) {
        return userMapper.findByUsernameLike("%"+username+"%");
    }

    @CacheEvict(value = "users", allEntries = true)
    @Override
    public int updateOne(User user) {
        User save = userMapper.save(user);
        if (save!=null){
            return 1;
        }else return 0;
    }
}
